package com.lxl.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lxl.dao.UserDAO;
import com.lxl.model.Tieba;
import com.lxl.model.User;

/**
 * 用户_用户DAO的实现
 * 
 * @author 刘星鹭
 * @Email liuxinglumail@163.com
 * @version 创建时间： 2014年9月9日 下午4:00:52
 */
@Transactional
@Repository("userDAO")
public class UserDAOImpl implements UserDAO {
    @Resource
    SessionFactory sessionFactory;// 注入会话工厂

    private String sqlFromUser = "from User";

    /**
     * 添加用户
     * 
     * @param user
     *            用户
     */
    @Override
    public void add(User user) {
        sessionFactory.getCurrentSession().persist(user);
    }

    /**
     * 删除用户
     * 
     * @param user
     *            用户
     */
    @Override
    public void delete(User user) {
        sessionFactory.getCurrentSession().delete(user);
    }

    /**
     * 更新用户
     * 
     * @param user
     *            用户
     */
    @Override
    public void update(User user) {
        sessionFactory.getCurrentSession().update(user);
    }

    /**
     * 通过用户名查找用户
     * 
     * @param userName
     *            用户名
     * @return 用户
     */
    @Override
    public User find(String userName) {
        return (User) sessionFactory.getCurrentSession().get(User.class,
                userName);
    }

    /**
     * 列出所有用户
     * 
     * @return 所有用户
     */
    @Override
    @SuppressWarnings("unchecked")
    public List<User> list() {
        return sessionFactory.getCurrentSession().createQuery(sqlFromUser)
                .list();
    }

    /**
     * 通过用户名列出该用户所有关注贴吧
     * 
     * @param userName
     *            用户名
     * @return 该用户所有关注贴吧
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Tieba> listTiebas(String userName) {
        return sessionFactory
                .getCurrentSession()
                .createSQLQuery(
                        "select tieba_name from t_user_tieba where user_name='"
                                + userName + "'").list();
    }

    /**
     * 通过用户名列出该用户所有帖子号
     * 
     * @param userName
     *            用户名
     * @return 该用户所有帖子号
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<String> listPostNos(String userName) {
        return sessionFactory
                .getCurrentSession()
                .createSQLQuery(
                        "select post_no from t_posts where user_name='"
                                + userName + "'").list();
    }

    /**
     * 列出当前页面10个用户
     * 
     * @param start
     *            页面号
     * @return 当前页面10个用户
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<User> listTen(int start) {
        List<User> list = sessionFactory.getCurrentSession()
                .createQuery("from User order by user_name asc").list();
        if (list.size() <= 10) {
            return list;
        } else if (start == 1) {
            return list.subList(0, 10);
        } else if (list.size() - 10 * (start - 1) < 10) {
            return list.subList(10 * (start - 1), list.size());
        }
        return list.subList(10 * (start - 1), 10 * (start - 1) + 10);
    }

    /**
     * 计算总页数
     * 
     * @return 总页数
     */
    @SuppressWarnings("unchecked")
    @Override
    public int pageCount() {
        List<User> list = sessionFactory.getCurrentSession()
                .createQuery(sqlFromUser).list();
        if (list.size() % 10 == 0) {
            return list.size() / 10;
        }
        return list.size() / 10 + 1;
    }

    /**
     * 为某用户添加喜欢的贴吧
     * 
     * @param user
     *            某用户
     * @param tieba
     *            喜欢的贴吧
     */
    @Override
    public void addFavourateTieba(User user, Tieba tieba) {
        user.getFavourateTiebas().add(tieba);
    }

    /**
     * 为某用户删除喜欢的贴吧
     * 
     * @param user
     *            某用户
     * @param tieba
     *            喜欢的贴吧
     */
    @Override
    public void deleteFavourateTieba(User user, Tieba tieba) {
        user.getFavourateTiebas().remove(tieba);
    }

    /**
     * 获得某用户未关注的贴吧列表
     * 
     * @param user
     *            某用户
     * @return 未关注的贴吧列表
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<String> getUnFavouratedTiebaName(User user) {
        List<String> allTiebaList = sessionFactory.getCurrentSession()
                .createSQLQuery("select tieba_name from t_tiebas").list();
        List<String> tiebaList = sessionFactory
                .getCurrentSession()
                .createSQLQuery(
                        "select tieba_name from t_user_tieba where user_name ='"
                                + user.getUserName() + "'").list();
        allTiebaList.removeAll(tiebaList);
        List<String> finalTiebaList = new ArrayList<String>();
        for (int i = 0; i < allTiebaList.size(); i++) {
            if (!finalTiebaList.contains(allTiebaList.get(i))) {
                finalTiebaList.add(allTiebaList.get(i));
            }
        }
        return finalTiebaList;
    }
}
